package ma.xin.shi;

import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.*;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
//import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.events.FocusAdapter;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import ma.xin.shi.configurations.dataType.DataTypeConfig;
import ma.xin.shi.configurations.dataType.DataTypeEntry;

import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.graphics.Point;
//import ma.xin.shi.ConfigurationManager;

public class DataTypeDisplay {
	private static Table dataListTable;              // 左侧数据列表表格
	private static Composite rightPanel;             // 右侧工作区父容器
	private static Composite rightCommonArea;        // 右侧公共区域，用于添加typeCombo和typeNameText
	private static Composite dynamicInputArea;       // 改成 static 是因为你用的是 static 方法
	private static Composite basicDynamicInputArea;  // basic 数据类型的动态区容器，针对subTypeCombo的不同值设计不同
	private static Composite rightConfirmArea;       // 右侧按钮区域，存放右侧按钮 confirmButton
	private static Button confirmButton;             // 确认按钮

	private static Combo typeCombo;                  // get method: getDataType , [ common ] data Type {"Primitive Type", "Array Type", "Record Type"}
	private static Text typeNameText;                // get method: getDataTypeName , [ common ] data type name to input
	private static Combo subTypeCombo;               // get method: getCategory ,  Primitive Type -> basic type 的子类型 {"VALUE", "BOOLEAN", "STRING", "ENUM"}
	private static Combo baseTypeCombo;              // get method: getBaseType ,  Primitive Type -> base type combo for {"boolean", "uint8", "uint16", "uint32", "uint64", "int8", "int16", "int32", "int64", "float", "double", "UTF-8"}
	private static Text minValueText;                // get method: getMinValue ,  Primitive Type -> value -> min value input
	private static Text maxValueText;                // get method: getMaxValue ,  Primitive Type -> value -> max value input
	private static Combo stringLengthTypeCombo;      // get method: getstringLengthType,  Primitive Type -> string -> string length type [Fixed, Dynamic]
	private static Spinner stringMinLengthSpinner;   // get method: getStringMinLengthSpinner ,  Primitive Type -> string -> string length
	private static Spinner stringMaxLengthSpinner;   // get method: getStringMaxLengthSpinner ,  Primitive Type -> string -> string max length
	private static Table enumTable;                  // get method: getFormattedEnumMembers ,  Primitive Type -> enum -> enum members table
	private static Combo arrayLengthTypeCombo;       // get method: getArrayLengthType ,  Array type -> array length type
	private static Text arrLengthMinText;            // get method: getArrayLengthMin ,  Array type -> array length min
	private static Text arrLengthMaxText;            // get method: getArrayLengthMax ,  Array type -> array length max
	private static Text arrMemberTypeText;           // get method: getArrayMemberType ,  Array type -> array basic type(member type)
	private static Table structTable;                // get method: getFormattedStructMembers ,  Record Type -> struct table
	
	public static void display(Composite workArea) {
        // 清空旧内容
        for (Control control : workArea.getChildren()) {
            control.dispose();
        }

        // 设置主容器为水平分割布局
        workArea.setLayout(new GridLayout(2, false)); // 两列，不自动等宽

        LeftDatalistColCtrl(workArea);
        RightPanelInit(workArea);
        createRightPanel(0, 0);

        workArea.layout(true, true);// 布局刷新
    }
	
	private static void LeftDatalistColCtrl(Composite parent) {
    	// ========== 左侧面板 ==========
        Composite leftPanel = new Composite(parent, SWT.BORDER);
        leftPanel.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE));

        leftPanel.setLayout(new GridLayout(1, false));
        GridData leftData = new GridData(SWT.FILL, SWT.FILL, false, true);
        leftData.widthHint = 200; // 假设宽度占总宽度的 1/4
        leftPanel.setLayoutData(leftData);

        Composite buttonPanel = new Composite(leftPanel, SWT.NONE);
        buttonPanel.setLayout(new GridLayout(2, false));
        buttonPanel.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false));

        Button addButton = new Button(buttonPanel, SWT.PUSH);
        addButton.setText("Add");
        addButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));

        Button deleteButton = new Button(buttonPanel, SWT.PUSH);
        deleteButton.setText("Del");
        deleteButton.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));

        // 添加数据类型清单表格
        addDataTypeDatalistTable(leftPanel);

        // ========= 添加按钮监听 =========
        addButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
            	createRightPanel(0, 0);
            }
        });

        // ========= 添加删除监听 =========
        deleteButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                int selectedIndex = dataListTable.getSelectionIndex();
                if (selectedIndex == -1) {
                    System.out.println("未选择任何条目进行删除");
                    return;
                }

                TableItem selectedItem = dataListTable.getItem(selectedIndex);
                DataTypeEntry entry = (DataTypeEntry) selectedItem.getData();
                String name = entry.getDataTypeName();

                boolean success = ConfigurationManager.getInstance()
                        .getDataTypeConfig()
                        .removeDataType(name);

                if (success) {
                    refreshDataTypeTable(); // 删除成功后刷新表格
                    System.out.println("已删除数据类型: " + name);
                } else {
                    System.out.println("删除失败");
                }
            }
        });
    }
	
	public static void refreshDataTypeTable() {
	    dataListTable.removeAll(); // 清空旧数据

	    List<DataTypeEntry> entries = ConfigurationManager
	            .getInstance()
	            .getDataTypeConfig()
	            .getAllDataTypes();

	    for (DataTypeEntry entry : entries) {
	        addDataTypeToTable(entry);
	    }
	}
	
	/**
	 * 将一个 DataTypeEntry 添加到左侧的数据列表表格中
	 */
	private static void addDataTypeToTable(DataTypeEntry entry) {
	    if (entry == null || entry.getDataTypeName() == null || entry.getDataTypeName().trim().isEmpty()) {
	        System.out.println("无法添加空或无名称的数据类型");
	        return;
	    }

	    TableItem item = new TableItem(dataListTable, SWT.NONE);
	    int index = dataListTable.indexOf(item); // 获取当前插入的位置索引
	    item.setText(0, String.valueOf(index + 1));         // 显示从1开始的序号
	    item.setText(1, entry.getDataTypeName());           // 数据类型名称
	    item.setData(entry);                                // 绑定对象以便后续操作使用
	}
	
	/* datalist 数据清单表格添加 页面加载的数据列表区域内容 */
	private static void addDataTypeDatalistTable(Composite parent) {
		// 创建表格控件
		dataListTable = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION);
		dataListTable.setHeaderVisible(true);  // 显示表头
		dataListTable.setLinesVisible(true);   // 显示表格线

	    // 设置布局数据：占满父容器剩余空间
	    GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
	    dataListTable.setLayoutData(gridData);

	    // 添加两列：Index 和 ECU Name
	    TableColumn indexColumn = new TableColumn(dataListTable, SWT.NONE);
	    indexColumn.setText("Index");
	    indexColumn.setWidth(50);  // 设置宽度

	    TableColumn nameColumn = new TableColumn(dataListTable, SWT.NONE);
	    nameColumn.setText("Name");
	    nameColumn.setWidth(150);  // 设置宽度

	    loadExistingDataTypesToTable(); //加载当前已经配置的所有数据

	    // ====== 表格项点击监听 ======
	    dataListTable.addSelectionListener(new SelectionAdapter() {
	        @Override
            public void widgetSelected(SelectionEvent e) {
	            TableItem[] selection = dataListTable.getSelection();
	            if (selection.length > 0) {
	                DataTypeEntry entry = (DataTypeEntry) selection[0].getData();
	                loadExistingDataTypeDetails(entry); // 加载已有数据详情
	            }
	        }
	    });
	}

	public static void loadExistingDataTypeDetails(DataTypeEntry entry) {
		System.out.println("Data Type Name: " + entry.getDataTypeName());
	    System.out.println("Data Type: " + entry.getDataType());
	    System.out.println("Category: " + entry.getCategory());
	    System.out.println("Base Type: " + entry.getBaseType());
	    System.out.println("Min Value: " + entry.getMinValue());
	    System.out.println("Max Value: " + entry.getMaxValue());
	    System.out.println("Array Length Type: " + entry.getArrayLengthType());
	    System.out.println("Array Length Min: " + entry.getArrayLengthMin());
	    System.out.println("Array Length Max: " + entry.getArrayLengthMax());
	    System.out.println("Array Member Type: " + entry.getArrayMemberType());
	    System.out.println("String Length Type: " + entry.getStringLengthType());
	    System.out.println("String Min Length: " + entry.getStringMinLength());
	    System.out.println("String Max Length: " + entry.getStringMaxLength());

	    // 根据 entry 数据重建控件
	    createInputsForDataType(entry);

	    // 切换按钮状态为 "Save"
	    confirmButton.setText("Save");

	    // 刷新布局
	    dynamicInputArea.layout(true, true);
	}

	private static void createInputsForDataType(DataTypeEntry entry) {
	    // Step 1: 获取并转换 type 和 baseType
	    int rootType = getRootTypeValue(entry);
	    int basicType = getBasicTypeValue(entry);
	    
	    System.out.println("===> rootType: " + rootType);
	    System.out.println("===> basicType: " + basicType);

	    // Step 2: 刷新整个右侧面板（先布局）
	    createRightPanel(rootType, basicType);

	    // Step 3: 布局完成后，再填充数据（后赋值）
	    if (entry != null) {
	        populateUIWithData(entry);
	    }
	}


	private static void loadExistingDataTypesToTable() {
	    // 1. 清空表格中的所有内容
	    if (dataListTable != null && dataListTable.getItems() != null) {
	        for (TableItem item : dataListTable.getItems()) {
	            item.dispose(); // 移除每个表项
	        }
	    }

	    // 2. 加载最新配置并更新表格
	    List<DataTypeEntry> allDataTypes = ConfigurationManager.getInstance().getDataTypeConfig().getAllDataTypes();

	    for (int i = 0; i < allDataTypes.size(); i++) {
	    	DataTypeEntry entry = allDataTypes.get(i);
	        TableItem item = new TableItem(dataListTable, SWT.NONE);
	        item.setText(0, String.valueOf(i + 1)); // 序号列，从1开始计数
	        item.setText(1, entry.getDataTypeName()); // 名称列
	        item.setData(entry); // 存储完整数据对象
	    }
	}

	private static void RightPanelInit(Composite parent) {
    	// ========== 右侧面板 ==========
    	rightPanel = new Composite(parent, SWT.BORDER);
        rightPanel.setLayout(new GridLayout(1, false));
        GridData rightData = new GridData(SWT.FILL, SWT.FILL, true, true);
        rightPanel.setLayoutData(rightData);
    }
	
	private static void createRightPanel(int RootType, int BasicType) {
	    if (rightPanel == null || rightPanel.isDisposed()) {
	        System.err.println("Right panel is null or disposed.");
	        return;
	    }

	    // Step 1: 清空所有子控件
	    for (Control control : rightPanel.getChildren()) {
	        control.dispose();
	    }

	    // Step 2: 创建 top 区域
	    createRightCommonPanel(rightPanel);

	    // Step 3: 创建 dynamicInputArea 放在下方
	    dynamicInputAreaInit(rightPanel);

	    // Step 4: 根据 RootType 创建具体输入内容
	    switch (RootType) {
	        case 0: // "Primitive Type":
	            updateDynamicInputArea(0, BasicType);
	            break;
	        case 1: // "Array Type":
	            updateDynamicInputArea(1, 0);
	            break;
	        case 2: // "Record Type":
	            updateDynamicInputArea(2, 0);
	            break;
	        default:
	            break;
	    }

	    createRightConfirmButton(rightPanel);
	    // Step 5: 刷新整体布局
	    rightPanel.layout(true, true);
	}

	private static void dynamicInputAreaInit(Composite parent) {
    	dynamicInputArea = new Composite(parent, SWT.NONE);
        dynamicInputArea.setLayout(new GridLayout(2, false));
        dynamicInputArea.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    }

	private static void createRightCommonPanel(Composite parent) {
	    if (rightCommonArea != null && !rightCommonArea.isDisposed()) {
	        for (Control control : rightCommonArea.getChildren()) {
	            control.dispose();
	        }
	        typeCombo = null;
	        typeNameText = null;
	    }

	    rightCommonArea = new Composite(parent, SWT.NONE);
	    rightCommonArea.setLayout(new GridLayout(2, false));
	    GridData commonData = new GridData(SWT.FILL, SWT.TOP, true, false);
	    commonData.grabExcessVerticalSpace = false;
	    rightCommonArea.setLayoutData(commonData);

	    Label typeLabel = new Label(rightCommonArea, SWT.NONE);
	    typeLabel.setText("Application Data Type:");

	    typeCombo = new Combo(rightCommonArea, SWT.DROP_DOWN | SWT.READ_ONLY);
	    typeCombo.setItems(new String[]{"Primitive Type", "Array Type", "Record Type"});
	    typeCombo.setVisibleItemCount(10);
	    typeCombo.select(0);
	    typeCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

	    Label dataTypeNameLabel = new Label(rightCommonArea, SWT.NONE);
	    dataTypeNameLabel.setText("Data Type Name:");
	    typeNameText = new Text(rightCommonArea, SWT.BORDER);
	    typeNameText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

	    // 在这里添加监听器
	    typeCombo.addSelectionListener(new SelectionAdapter() {
	        @Override
	        public void widgetSelected(SelectionEvent e) {
	            int selectedIndex = typeCombo.getSelectionIndex();
	            updateDynamicInputArea(selectedIndex, 0);
	        }
	    });

	    //parent.layout(true, true);
	}
	
	private static void updateDynamicInputArea(int selectedTypeIndex, int selectedSubTypeIndex) {
	    // 清空 dynamicInputArea
	    for (Control control : dynamicInputArea.getChildren()) {
	        control.dispose();
	    }

	    // 根据选中的类型创建新的控件
	    switch (selectedTypeIndex) {
	        case 0: // Primitive Type
	            createBasicTypeInputs(dynamicInputArea, selectedSubTypeIndex);
	            break;
	        case 1: // Array Type
	            createArrayTypeInputs(dynamicInputArea);
	            break;
	        case 2: // Record Type
	            createStructTypeInputs(dynamicInputArea);
	            break;
	        default:
	            break;
	    }
	    
	    // 刷新布局
	    dynamicInputArea.layout(true, true);
	}

	private static void createRightConfirmButton(Composite parent) {
    	rightConfirmArea = new Composite(parent, SWT.NONE);
	    RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
	    rowLayout.spacing = 5;
	    rightConfirmArea.setLayout(rowLayout);

	    GridData buttonData = new GridData(SWT.END, SWT.BOTTOM, true, false);
	    rightConfirmArea.setLayoutData(buttonData);

    	confirmButton = new Button(rightConfirmArea, SWT.PUSH);
    	confirmButton.setText("Add");

    	Button cancelButton = new Button(rightConfirmArea, SWT.PUSH);
    	cancelButton.setText("Cancel");

    	// ===== 绑定按钮行为 =====
    	confirmButton.addSelectionListener(new SelectionAdapter() {
    	    @Override
    	    public void widgetSelected(SelectionEvent e) {
    	        if ("Add".equals(confirmButton.getText())) {
    	            addNewDataType(); // 自定义新增逻辑
    	        } else if ("Save".equals(confirmButton.getText())) {
    	            saveEditedDataType(); // 自定义保存逻辑
    	        }
    	    }
    	});

    	cancelButton.addSelectionListener(new SelectionAdapter() {
    	    @Override
    	    public void widgetSelected(SelectionEvent e) {
    	    }
    	});
    }
	
	public static void saveEditedDataType() {
        int selectedIndex = dataListTable.getSelectionIndex();
        if (selectedIndex == -1) {
            System.out.println("未选择任何条目进行编辑");
            return;
        }

        TableItem selectedItem = dataListTable.getItem(selectedIndex);
        DataTypeEntry oldEntry = (DataTypeEntry) selectedItem.getData();
        String oldName = oldEntry.getDataTypeName();

        // 1. 获取用户输入的新数据
        DataTypeEntry updatedEntry = getNewDataTypeEntryFromUI();
        if (updatedEntry == null) {
            System.out.println("输入无效，无法保存");
            return;
        }

        // 2. 更新配置
        DataTypeConfig dataTypeConfig = ConfigurationManager.getInstance().getDataTypeConfig();
        boolean success = dataTypeConfig.updateDataType(oldName, updatedEntry);

        if (success) {
            // 3. 更新表格显示
            selectedItem.setText(0, String.valueOf(selectedIndex + 1));
            selectedItem.setText(1, updatedEntry.getDataTypeName());
            selectedItem.setData(updatedEntry); // 更新绑定的数据对象

            System.out.println("已保存修改: " + updatedEntry.getDataTypeName());
        } else {
            System.out.println("保存失败，未找到匹配的条目");
        }
    }
	
	public static void addNewDataType() {
        // 1. 从 UI 获取当前输入的新 DataTypeEntry
        DataTypeEntry newEntry = getNewDataTypeEntryFromUI();

        if (newEntry == null) {
            System.out.println("输入无效，无法添加");
            return;
        }

        System.out.println("newEntry==> Data Type Name: " + newEntry.getDataTypeName());
        System.out.println("newEntry==> Data Type: " + newEntry.getDataType());
        System.out.println("newEntry==> Category: " + newEntry.getCategory());
        System.out.println("newEntry==> Base Type: " + newEntry.getBaseType());
        System.out.println("newEntry==> Min Value: " + newEntry.getMinValue());
        System.out.println("newEntry==> Max Value: " + newEntry.getMaxValue());
        System.out.println("newEntry==> Array Length Type: " + newEntry.getArrayLengthType());
        System.out.println("newEntry==> Array Length Min: " + newEntry.getArrayLengthMin());
        System.out.println("newEntry==> Array Length Max: " + newEntry.getArrayLengthMax());
        System.out.println("newEntry==> Array Member Type: " + newEntry.getArrayMemberType());
        System.out.println("newEntry==> String Length Type: " + newEntry.getStringLengthType());
        System.out.println("newEntry==> String Min Length: " + newEntry.getStringMinLength());
        System.out.println("newEntry==> String Max Length: " + newEntry.getStringMaxLength());
        
        // 2. 获取全局配置对象并添加
        DataTypeConfig dataTypeConfig = ConfigurationManager.getInstance().getDataTypeConfig();
        dataTypeConfig.addDataType(newEntry);

        // 3. 添加到左侧表格中
        addDataTypeToTable(newEntry);

        System.out.println("新增数据类型成功：" + newEntry.getDataTypeName());
    }
	
	public static DataTypeEntry getNewDataTypeEntryFromUI() {
	    // 检查 typeNameText 是否为空
	    if (typeNameText == null || typeNameText.isDisposed()) {
	        System.err.println("Error: typeNameText is null or disposed.");
	        return null;
	    }

	    String name = typeNameText.getText().trim();
	    if (name.isEmpty()) {
	        System.out.println("输入无效，无法保存：名称不能为空");
	        return null; // 名称不能为空
	    }

	    DataTypeEntry entry = new DataTypeEntry();
	    entry.setDataTypeName(name);

	    // 设置 data type
	    if (typeCombo != null && !typeCombo.isDisposed()) {
	        String dataType = typeCombo.getText();
	        entry.setDataType(dataType);

	        if ("Primitive Type".equals(dataType)) {
	            handlePrimitiveType(entry);
	        } else if ("Array Type".equals(dataType)) {
	            handleArrayType(entry);
	        } else if ("Record Type".equals(dataType)) {
	            handleStructType(entry);
	        }
	    } else {
	        System.err.println("Error: typeCombo is null or disposed.");
	    }

	    return entry;
	}

	private static void handlePrimitiveType(DataTypeEntry entry) {
	    if (subTypeCombo == null || subTypeCombo.isDisposed()) {
	        System.err.println("Error: subTypeCombo is null or disposed.");
	        return;
	    }

	    String subType = subTypeCombo.getText();
	    //entry.setCategory(subType.toLowerCase());
	    entry.setCategory(subTypeCombo.getText());

	    switch (subType) {
	        case "VALUE":
	            if(baseTypeCombo != null && !baseTypeCombo.isDisposed()){
	                entry.setBaseType(baseTypeCombo.getText());
	            }
	            if(minValueText != null){
	                entry.setMinValue(minValueText.getText().trim());
	            }
	            if(maxValueText != null){
	                entry.setMaxValue(maxValueText.getText().trim());
	            }
	            break;

	        case "BOOLEAN":
	            entry.setBaseType("boolean"); // 因为布尔类型没有其他选择，这里保留固定设置
	            break;

	        case "STRING":
	            if(stringLengthTypeCombo != null && !stringLengthTypeCombo.isDisposed()){
	                entry.setStringLengthType(stringLengthTypeCombo.getText());
	            }
	            if (stringMinLengthSpinner != null && !stringMinLengthSpinner.isDisposed()) {
	                int currentMinLength = stringMinLengthSpinner.getSelection();
	                entry.setStringMinLength(currentMinLength);
	            }

	            if (stringMaxLengthSpinner != null && !stringMaxLengthSpinner.isDisposed()) {
	                int currentMaxLength = stringMaxLengthSpinner.getSelection();
	                entry.setStringMaxLength(currentMaxLength);
	            }

	            break;

	        case "ENUM":
	            if(enumTable != null && !enumTable.isDisposed()){
	                for (TableItem item : enumTable.getItems()) {
	                    String enumName = item.getText(1); // Name
	                    String enumValue = item.getText(2); // Value
	                    entry.addEnumMember(enumName, enumValue);
	                }
	            }
	            break;

	        default:
	            System.out.println("Unknown primitive category: " + subType);
	            break;
	    }
	}

	private static void handleArrayType(DataTypeEntry entry) {
	    if (arrayLengthTypeCombo != null && !arrayLengthTypeCombo.isDisposed()) {
	        entry.setArrayLengthType(arrayLengthTypeCombo.getText());
	    }
	    if (arrLengthMinText != null && !arrLengthMinText.isDisposed()) {
	        entry.setArrayLengthMin(arrLengthMinText.getText().trim());
	    }
	    if (arrLengthMaxText != null && !arrLengthMaxText.isDisposed()) {
	        entry.setArrayLengthMax(arrLengthMaxText.getText().trim());
	    }
	    if (arrMemberTypeText != null && !arrMemberTypeText.isDisposed()) {
	        entry.setArrayMemberType(arrMemberTypeText.getText().trim());
	    }
	}

	private static void handleStructType(DataTypeEntry entry) {
	    if (structTable == null || structTable.isDisposed()) {
	        System.err.println("Error: structTable is null or disposed.");
	        return;
	    }

	    for (TableItem item : structTable.getItems()) {
	    	System.out.println(item.getText(0));
	    	System.out.println(item.getText(1));
	    	System.out.println(item.getText(2));
	    	String memberName = item.getText(1);
	        String memberType = item.getText(2);
	        entry.addStructMember(memberName, memberType);
	    }
	}

	
	private static void createBasicTypeInputs(final Composite parent, int BasicType) {
	    // 初始化基本控件
	    createBasicCommonInputs(parent);
	    // 添加Category选择监听器
	    subTypeCombo.addSelectionListener(new SelectionAdapter() {
	        @Override
	        public void widgetSelected(SelectionEvent e) {
	            // 清除之前的内容
	        	for (Control control : basicDynamicInputArea.getChildren()) {
	                control.dispose();
	            }

	            switch (subTypeCombo.getSelectionIndex()) {
	                case 0: // VALUE
	                	setBaseTypeAndNotify("uint8");
	                    basicSubTypeVALUECtrl();
	                    break;
	                case 1: // BOOLEAN
	                	setBaseTypeAndNotify("boolean");
	                    basicSubTypeBOOLEANCtrl();
	                    break;
	                case 2: // STRING
	                	setBaseTypeAndNotify("UTF-8");
	                    basicSubTypeSTRINGCtrl();
	                    break;
	                case 3: // public
	                	
	                	setBaseTypeAndNotify("uint32");
	                    basicSubTypeENUMCtrl();
	                    break;
	                default:
	                    break;
	            }
	            // 强制重新布局
	            basicDynamicInputArea.layout(true, true);
	            parent.layout(true, true); // 刷新父容器布局
	        }
	    });

	    // 设置默认选中项
	    if (subTypeCombo.getItemCount() > 0) {
	        subTypeCombo.select(BasicType);

	        // 手动触发 Selection 事件
	        Event event = new Event();
	        event.widget = subTypeCombo;
	        event.detail = SWT.NONE;
	        subTypeCombo.notifyListeners(SWT.Selection, event);
	    }
	}
	
	private static void setBaseTypeAndNotify(String value) {
	    if (baseTypeCombo == null || baseTypeCombo.isDisposed()) {
	        return;
	    }

	    int index = baseTypeCombo.indexOf(value);
	    if (index >= 0) {
	        baseTypeCombo.select(index);

	        // 可选：通知监听器（如果 baseTypeCombo 有其他联动逻辑）
	        Event event = new Event();
	        event.widget = baseTypeCombo;
	        baseTypeCombo.notifyListeners(SWT.Selection, event);
	    }
	}
	
	private static void basicSubTypeVALUECtrl() {
	    if (minValueText != null && !minValueText.isDisposed()) {
	        return; // 如果控件已存在，则直接返回
	    }

	    Label minValueLabel = new Label(basicDynamicInputArea, SWT.NONE);
	    minValueLabel.setText("Min Value:");
	    minValueText = new Text(basicDynamicInputArea, SWT.BORDER);
	    GridData minValuetextData = new GridData(SWT.FILL, SWT.CENTER, true, false);
	    minValueText.setLayoutData(minValuetextData);

	    Label maxValueLabel = new Label(basicDynamicInputArea, SWT.NONE);
	    maxValueLabel.setText("Max Value:");
	    maxValueText = new Text(basicDynamicInputArea, SWT.BORDER);
	    GridData maxValuetextData = new GridData(SWT.FILL, SWT.CENTER, true, false);
	    maxValueText.setLayoutData(maxValuetextData);
	}

	private static void basicSubTypeBOOLEANCtrl() {
    	// do nothing
    }

	private static void basicSubTypeSTRINGCtrl() {
    	// STRINGLengthType
        Label stringLengthTypeLabel = new Label(basicDynamicInputArea, SWT.NONE);
        stringLengthTypeLabel.setText("String Length Type:");
        stringLengthTypeCombo = new Combo(basicDynamicInputArea, SWT.DROP_DOWN | SWT.READ_ONLY);
        stringLengthTypeCombo.setItems(new String[]{"Fixed", "Dynamic"});
        stringLengthTypeCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        stringLengthTypeCombo.select(1);

        // STRING Length
        Label stringLengthLabel = new Label(basicDynamicInputArea, SWT.NONE);
        stringLengthLabel.setText("String Length:");
        stringMinLengthSpinner = new Spinner(basicDynamicInputArea, SWT.BORDER);
        stringMinLengthSpinner.setMinimum(0);
        stringMinLengthSpinner.setMaximum(65535);
        stringMinLengthSpinner.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        // Max Length
        Label maxLengthLabel = new Label(basicDynamicInputArea, SWT.NONE);
        maxLengthLabel.setText("Max Length:");
        stringMaxLengthSpinner = new Spinner(basicDynamicInputArea, SWT.BORDER);
        stringMaxLengthSpinner.setMinimum(0);
        stringMaxLengthSpinner.setMaximum(65535);
        stringMaxLengthSpinner.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
    }

	private static void basicSubTypeENUMCtrl() {
    	Label enumTableLabel = new Label(basicDynamicInputArea, SWT.NONE);
        enumTableLabel.setText("ENUM members:");

        GridData labelData = new GridData(SWT.FILL, SWT.CENTER, true, false);
        labelData.horizontalSpan = 2; // 占两列
        enumTableLabel.setLayoutData(labelData);

        enumTable = new Table(basicDynamicInputArea, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
        enumTable.setHeaderVisible(true);
        enumTable.setLinesVisible(true);

        System.out.println("初始列数: " + enumTable.getColumnCount()); // 应该是1（默认列）
        while (enumTable.getColumnCount() > 0) {
            enumTable.getColumns()[0].dispose();
        }
        System.out.println("清除后列数: " + enumTable.getColumnCount()); // 应该是0

        GridData tableData = new GridData(SWT.FILL, SWT.FILL, true, false);
        tableData.horizontalSpan = 2;
        tableData.heightHint = 150;
        enumTable.setLayoutData(tableData);

        String[] columnNames = {"Index", "Name", "Value"};
        int[] columnWidths = {60, 150, 100};

        for (int i = 0; i < columnNames.length; i++) {
        	System.out.println("current col: " + i);
            TableColumn column = new TableColumn(enumTable, SWT.NONE);
            column.setText(columnNames[i]);
            column.setWidth(columnWidths[i]);
        }

        System.out.println("Number of columns after initialization: " + enumTable.getColumnCount());

        // 创建按钮容器
        Composite buttonComposite = new Composite(basicDynamicInputArea, SWT.NONE);
        RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
        rowLayout.spacing = 5; // 控制两个按钮之间的间距
        rowLayout.marginLeft = 0;
        rowLayout.marginRight = 0;
        rowLayout.marginTop = 0;
        rowLayout.marginBottom = 0;
        buttonComposite.setLayout(rowLayout);

        GridData buttonData = new GridData(SWT.END, SWT.CENTER, false, false);
        buttonData.horizontalSpan = 2;
        buttonComposite.setLayoutData(buttonData);

        // 添加按钮
        Button btnAdd = new Button(buttonComposite, SWT.PUSH);
        btnAdd.setText("Add");

        Button btnRemove = new Button(buttonComposite, SWT.PUSH);
        btnRemove.setText("Del");

        // 新增按钮逻辑
        btnAdd.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                int index = enumTable.getItemCount();
                TableItem item = new TableItem(enumTable, SWT.NONE);
                item.setText(0, String.valueOf(index));
                item.setText(1, "name" + index);
                item.setText(2, String.valueOf(index));
            }
        });

        // 删除按钮逻辑
        btnRemove.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                TableItem[] selectedItems = enumTable.getSelection();
                for (TableItem item : selectedItems) {
                    item.dispose();
                }
                renumberTableItems(enumTable); // 更新索引
            }
        });

        // 双击编辑逻辑
        enumTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseDoubleClick(MouseEvent e) {
                editTableCell(enumTable);
            }
        });
    }
	
	private static void editTableCell(final Table table) {
	    table.addMouseListener(new MouseAdapter() {
	        @Override
	        public void mouseDoubleClick(MouseEvent e) {
	            Point pt = new Point(e.x, e.y);
	            TableItem item = table.getItem(pt);
	            if (item == null) return;

	            int columnCount = table.getColumnCount();
	            int x = e.x;
	            int currentX = 0;
	            int col = -1;

	            for (int i = 0; i < columnCount; i++) {
	                TableColumn column = table.getColumn(i);
	                int width = column.getWidth();
	                if (x >= currentX && x <= currentX + width) {
	                    col = i;
	                    break;
	                }
	                currentX += width;
	            }

	            System.out.println("点击的列为：" + col + " | 当前列数：" + table.getColumnCount());

	            if (col == -1 || col >= columnCount) {
	                return;
	            }

	            if (col == 1 || col == 2) {
	                final Text text = new Text(table, SWT.NONE);
	                final TableEditor editor = new TableEditor(table);
	                editor.grabHorizontal = true;
	                editor.setEditor(text, item, col);
	                text.setText(item.getText(col));
	                text.selectAll();
	                text.setFocus();

	                final int finalCol = col;

	                text.addKeyListener(new KeyAdapter() {
	                    @Override
	                    public void keyReleased(KeyEvent ke) {
	                        if (ke.character == SWT.CR) {
	                            String newText = text.getText().trim();
	                            if (!newText.isEmpty() && !item.isDisposed()) {
	                                item.setText(finalCol, newText);
	                            }
	                            disposeEditorAndText(editor, text);
	                        } else if (ke.character == SWT.ESC) {
	                            disposeEditorAndText(editor, text);
	                        }
	                    }
	                });

	                text.addFocusListener(new FocusAdapter() {
	                    @Override
	                    public void focusLost(FocusEvent e) {
	                        String newText = text.getText().trim();
	                        if (!newText.isEmpty() && !item.isDisposed()) {
	                            item.setText(finalCol, newText);
	                        }
	                        disposeEditorAndText(editor, text);
	                    }
	                });
	            }
	        }
	    });
	}
	
	private static void disposeEditorAndText(TableEditor editor, Text text) {
	    if (editor != null) editor.dispose();
	    if (text != null && !text.isDisposed()) text.dispose();
	}

	private static void renumberTableItems(Table table) {
        for (int i = 0; i < table.getItemCount(); i++) {
            TableItem item = table.getItem(i);
            item.setText(0, String.valueOf(i));
        }
    }
	
	private static void createArrayTypeInputs(Composite parent) {
        // add array Length Type
        Label arrayLengthTypeLable = new Label(parent, SWT.NONE);
        arrayLengthTypeLable.setText("arrayLengthType:");
        arrayLengthTypeCombo = new Combo(parent, SWT.DROP_DOWN | SWT.READ_ONLY);
        arrayLengthTypeCombo.setItems(new String[]{"Fixed", "Dynamic"});
        arrayLengthTypeCombo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        // add array Length Min
        Label arrLengthMinLabel = new Label(parent, SWT.NONE);
        arrLengthMinLabel.setText("arrayLengthMin:");
        arrLengthMinText = new Text(parent, SWT.BORDER);
        arrLengthMinText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        // add array Length Max
        Label arrLengthMaxLabel = new Label(parent, SWT.NONE);
        arrLengthMaxLabel.setText("arrayLengthMax:");
        arrLengthMaxText = new Text(parent, SWT.BORDER);
        arrLengthMaxText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));

        // add array Member Data Type
        Label arrMemberTypelabel = new Label(parent, SWT.NONE);
        arrMemberTypelabel.setText("arrayMemberType:");
        arrMemberTypeText = new Text(parent, SWT.BORDER);
        arrMemberTypeText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
        
        // --------------- 关键修改：添加判断，打破无限循环 ---------------
        // 1. 先判断typeCombo是否有效（非null且未销毁）
        if (typeCombo != null && !typeCombo.isDisposed()) {
            // 2. 仅当当前选中索引不是“Array Type”（索引1）时，才执行后续操作
            if (typeCombo.getSelectionIndex() != 1) {
                typeCombo.select(1); // 选中“Array Type”
                // 3. 触发事件（此时不会再次进入循环，因为选中索引已为1）
                Event event = new Event();
                event.widget = typeCombo; // 明确事件源
                typeCombo.notifyListeners(SWT.Selection, event);
            }
        }
    }
	
	private static void createStructTypeInputs(Composite parent) {
    	/* add struct table */
    	// add struct table Title
    	Label structTableLabel = new Label(parent, SWT.NONE);
    	structTableLabel.setText("Struct members:");

    	GridData labelData = new GridData(SWT.FILL, SWT.CENTER, true, false);
        labelData.horizontalSpan = 2; // 占两列
        structTableLabel.setLayoutData(labelData);
    	
    	// add struct table
    	structTable = new Table(parent, SWT.BORDER | SWT.FULL_SELECTION | SWT.MULTI);
    	structTable.setHeaderVisible(true);
    	structTable.setLinesVisible(true);
    	
    	GridData tableData = new GridData(SWT.FILL, SWT.FILL, true, false);
        tableData.horizontalSpan = 1;
        tableData.heightHint = 150;
        structTable.setLayoutData(tableData);

        String[] columnNames = {"index", "memberName", "memberType"};
        int[] columnWidths = {60, 150, 100};

        for (int i = 0; i < columnNames.length; i++) {
            TableColumn column = new TableColumn(structTable, SWT.NONE);
            column.setText(columnNames[i]);
            column.setWidth(columnWidths[i]);
        }
        
        // add button [Add , Del] , composite Row
        Composite buttonComposite = new Composite(parent, SWT.NONE);
        RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
        rowLayout.spacing = 5; // space within button Add and button Del
        rowLayout.marginLeft = 0;
        rowLayout.marginRight = 0;
        rowLayout.marginTop = 0;
        rowLayout.marginBottom = 0;
        buttonComposite.setLayout(rowLayout);

        GridData buttonData = new GridData(SWT.END, SWT.CENTER, false, false);
        buttonData.horizontalSpan = 2;
        buttonComposite.setLayoutData(buttonData);

        // button Add
        Button btnAdd = new Button(buttonComposite, SWT.PUSH);
        btnAdd.setText("Add");
        // button Del
        Button btnRemove = new Button(buttonComposite, SWT.PUSH);
        btnRemove.setText("Del");

        // add button [Add , Del] logic
        btnAdd.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
        	System.out.println("add struct");
            int index = structTable.getItemCount();
            TableItem item = new TableItem(structTable, SWT.NONE);
            item.setText(0, String.valueOf(index));
            item.setText(1, "name" + index);
            item.setText(2, String.valueOf(index));
            }
        });
        
        // button Del logic
        btnRemove.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                TableItem[] selectedItems = structTable.getSelection();
                for (TableItem item : selectedItems) {
                    item.dispose();
                }
                renumberTableItems(structTable); // 更新索引
            }
        });

        // 双击编辑逻辑
        structTable.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseDoubleClick(MouseEvent e) {
               editTableCell(structTable);
            }
        });
        
        // 选中下拉选项中的"Record Type"（索引2）
        // 添加判断避免无限递归
        if (typeCombo != null && !typeCombo.isDisposed()) {
            // 只有当前未选中Record Type时才执行操作
            if (typeCombo.getSelectionIndex() != 2) {
                typeCombo.select(2);
                
                // 触发事件并指定事件源
                Event event = new Event();
                event.widget = typeCombo;
                typeCombo.notifyListeners(SWT.Selection, event);
            }
        }
    }
	
	private static void createBasicCommonInputs(Composite parent) {
	    System.out.println("=====================>createBasicCommonInputs enter");

	    // add Category
	    Label subTypeLabel = new Label(parent, SWT.NONE);
	    subTypeLabel.setText("Category:");

	    subTypeCombo = new Combo(parent, SWT.DROP_DOWN | SWT.READ_ONLY);
	    subTypeCombo.setItems(new String[]{"VALUE", "BOOLEAN", "STRING", "ENUM"});

	    // set Category max width
	    GridData comboData = new GridData(SWT.LEFT, SWT.CENTER, false, false);
	    comboData.widthHint = 100;
	    subTypeCombo.setLayoutData(comboData);

	    // add BaseType
	    Label baseTypeLabel = new Label(parent, SWT.NONE);
	    baseTypeLabel.setText("Base Type:");

	    baseTypeCombo = new Combo(parent, SWT.DROP_DOWN | SWT.READ_ONLY);
	    baseTypeCombo.setItems(new String[]{
	        "boolean", "uint8", "uint16", "uint32", "uint64",
	        "int8", "int16", "int32", "int64", "float", "double", "UTF-8"
	    });

	    // set BaseType max width
	    GridData baseTypeData = new GridData(SWT.LEFT, SWT.CENTER, false, false);
	    baseTypeData.widthHint = 100;
	    baseTypeCombo.setLayoutData(baseTypeData);

	    // 检查并清理 old basicDynamicInputArea
	    if (basicDynamicInputArea != null && !basicDynamicInputArea.isDisposed()) {
	        for (Control control : basicDynamicInputArea.getChildren()) {
	            control.dispose();
	        }
	        basicDynamicInputArea.dispose();
	    }

	    // 添加一个占位符Composite用于动态添加控件
	    basicDynamicInputArea = new Composite(parent, SWT.NONE);
	    GridLayout layout = new GridLayout(2, false); // 设置为两列
	    layout.marginHeight = 0;
	    layout.marginWidth = 0;
	    basicDynamicInputArea.setLayout(layout);
	    GridData basicDynamicInputData = new GridData(SWT.FILL, SWT.TOP, true, false);
	    basicDynamicInputArea.setLayoutData(basicDynamicInputData);
	}

	
	private static int getRootTypeValue(DataTypeEntry entry) {
	    if (entry == null || entry.getDataType() == null) return 0;

	    switch (entry.getDataType()) {
	        case "Primitive Type":
	            return 0;
	        case "Array Type":
	            return 1;
	        case "Record Type":
	            return 2;
	        default:
	            return 0;
	    }
	}

	private static int getBasicTypeValue(DataTypeEntry entry) {
	    if (entry == null || entry.getCategory() == null) return 0;

	    switch (entry.getCategory()) {
	        case "VALUE":
	            return 0;
	        case "BOOLEAN":
	            return 1;
	        case "STRING":
	            return 2;
	        case "ENUM":
	            return 3;
	        default:
	            return 0;
	    }
	}

	private static void populateUIWithData(DataTypeEntry entry) {
	    if (typeNameText != null && !typeNameText.isDisposed()) {
	        typeNameText.setText(entry.getDataTypeName());
	    }

	    // 根据 dataType 显示不同区域的内容
	    switch (entry.getDataType()) {
	        case "Primitive Type":
	            fillPrimitiveData(entry);
	            break;
	        case "Array Type":
	            fillArrayData(entry);
	            break;
	        case "Record Type":
	            fillStructData(entry);
	            break;
	        default:
	            break;
	    }

	    // 可选：如果还有枚举、结构体等复杂字段，可以单独处理
	}

	private static void fillPrimitiveData(DataTypeEntry entry) {
	    if (entry == null) return;

	    fillCommonRightInfo(entry);

	    String category = entry.getCategory();
	    // 根据 category 调用对应的子类型填充方法
	    switch (category) {
	        case "VALUE":
	            fillValueSubType(entry);
	            break;
	        case "BOOLEAN":
	            fillBooleanSubType(entry);
	            break;
	        case "STRING":
	            fillStringSubType(entry);
	            break;
	        case "ENUM":
	            fillEnumSubType(entry);
	            break;
	        default:
	            System.out.println("Unknown category: " + category);
	            break;
	    }
	}

	private static void fillCommonRightInfo(DataTypeEntry entry) {
		// 填充 typeNameText
	    if (typeNameText != null && !typeNameText.isDisposed()) {
	        typeNameText.setText(entry.getDataTypeName());
	    }

	    // 设置 subTypeCombo
	    String getDataType = entry.getCategory();
	    if (typeCombo != null && !typeCombo.isDisposed()) {
	        for (int i = 0; i < typeCombo.getItemCount(); i++) {
	            if (typeCombo.getItem(i).equals(getDataType)) {
	            	typeCombo.select(i);
	                break;
	            }
	        }
	    }
	}

	private static void fillValueSubType(DataTypeEntry entry) {
	    // fill subTypeCombo
		String subType = entry.getCategory();
	    if (subTypeCombo != null && !subTypeCombo.isDisposed()) {
	        for (int i = 0; i < subTypeCombo.getItemCount(); i++) {
	            if (subTypeCombo.getItem(i).equals(subType)) {
	                subTypeCombo.select(i);
	                break;
	            }
	        }
	    }

	    // baseTypeCombo
	    String baseType = entry.getBaseType();
	    if (baseTypeCombo != null && !baseTypeCombo.isDisposed()) {
	        for (int i = 0; i < baseTypeCombo.getItemCount(); i++) {
	            if (baseTypeCombo.getItem(i).equals(baseType)) {
	            	baseTypeCombo.select(i);
	                break;
	            }
	        }
	    }

	    // minValueText
	    if (minValueText != null && !minValueText.isDisposed()) {
	    	minValueText.setText(entry.getMinValue());
	    }

	    // maxValueText
	    if (maxValueText != null && !maxValueText.isDisposed()) {
	    	maxValueText.setText(entry.getMaxValue());
	    }
	}

	private static void fillBooleanSubType(DataTypeEntry entry) {
		// fill subTypeCombo
		String subType = entry.getCategory();
	    if (subTypeCombo != null && !subTypeCombo.isDisposed()) {
	        for (int i = 0; i < subTypeCombo.getItemCount(); i++) {
	            if (subTypeCombo.getItem(i).equals(subType)) {
	                subTypeCombo.select(i);
	                break;
	            }
	        }
	    }

	    // baseTypeCombo
	    String baseType = entry.getBaseType();
	    if (baseTypeCombo != null && !baseTypeCombo.isDisposed()) {
	        for (int i = 0; i < baseTypeCombo.getItemCount(); i++) {
	            if (baseTypeCombo.getItem(i).equals(baseType)) {
	            	baseTypeCombo.select(i);
	                break;
	            }
	        }
	    }
	}

	private static void fillStringSubType(DataTypeEntry entry) {
		// fill subTypeCombo
		String subType = entry.getCategory();
	    if (subTypeCombo != null && !subTypeCombo.isDisposed()) {
	        for (int i = 0; i < subTypeCombo.getItemCount(); i++) {
	            if (subTypeCombo.getItem(i).equals(subType)) {
	                subTypeCombo.select(i);
	                break;
	            }
	        }
	    }

	    // baseTypeCombo
	    String baseType = entry.getBaseType();
	    if (baseTypeCombo != null && !baseTypeCombo.isDisposed()) {
	        for (int i = 0; i < baseTypeCombo.getItemCount(); i++) {
	            if (baseTypeCombo.getItem(i).equals(baseType)) {
	            	baseTypeCombo.select(i);
	                break;
	            }
	        }
	    }
	    
	    //stringLengthTypeCombo
	    String stringLengthType = entry.getStringLengthType();
	    if (stringLengthTypeCombo != null && !stringLengthTypeCombo.isDisposed()) {
	        for (int i = 0; i < stringLengthTypeCombo.getItemCount(); i++) {
	            if (stringLengthTypeCombo.getItem(i).equals(stringLengthType)) {
	            	stringLengthTypeCombo.select(i);
	                break;
	            }
	        }
	    }

	    //stringMinLengthSpinner
	    if (stringMinLengthSpinner != null && !stringMinLengthSpinner.isDisposed()) {
	        int minLength = entry.getStringMinLength();
	        stringMinLengthSpinner.setSelection(minLength);
	    }

	    
	    //stringMaxLengthSpinner
	    if (stringMaxLengthSpinner != null && !stringMaxLengthSpinner.isDisposed()) {
	        int minLength = entry.getStringMinLength();
	        stringMaxLengthSpinner.setSelection(minLength);
	    }

	}

	private static void fillEnumSubType(DataTypeEntry entry) {
		// subTypeCombo
		String subType = entry.getCategory();
	    if (subTypeCombo != null && !subTypeCombo.isDisposed()) {
	        for (int i = 0; i < subTypeCombo.getItemCount(); i++) {
	            if (subTypeCombo.getItem(i).equals(subType)) {
	                subTypeCombo.select(i);
	                break;
	            }
	        }
	    }

	    // baseTypeCombo
	    String baseType = entry.getBaseType();
	    if (baseTypeCombo != null && !baseTypeCombo.isDisposed()) {
	        for (int i = 0; i < baseTypeCombo.getItemCount(); i++) {
	            if (baseTypeCombo.getItem(i).equals(baseType)) {
	            	baseTypeCombo.select(i);
	                break;
	            }
	        }
	    }

	    // enumTable
	    List<String[]> members = entry.getFormattedEnumMembers();
	    for (String[] member : members) {
	        TableItem item = new TableItem(enumTable, SWT.NONE);
	        item.setText(new String[]{member[0], member[1], member[2]});
	    }
	}

	private static void fillArrayData(DataTypeEntry entry) {
		// arrayLengthTypeCombo
		String arrayLengthType = entry.getArrayLengthType();
	    if (arrayLengthTypeCombo != null && !arrayLengthTypeCombo.isDisposed()) {
	        for (int i = 0; i < arrayLengthTypeCombo.getItemCount(); i++) {
	            if (arrayLengthTypeCombo.getItem(i).equals(arrayLengthType)) {
	            	arrayLengthTypeCombo.select(i);
	                break;
	            }
	        }
	    }

		// arrLengthMinText
	    if (arrLengthMinText != null && !arrLengthMinText.isDisposed()) {
	    	arrLengthMinText.setText(entry.getArrayLengthMin());
	    }

	    // arrLengthMaxText
	    if (arrLengthMaxText != null && !arrLengthMaxText.isDisposed()) {
	    	arrLengthMaxText.setText(entry.getArrayLengthMax());
	    }
	    
		// arrMemberTypeText
	    if (arrMemberTypeText != null && !arrMemberTypeText.isDisposed()) {
	    	arrMemberTypeText.setText(entry.getArrayMemberType());
	    }
	}

	private static void fillStructData(DataTypeEntry entry) {
		// structTable
		List<String[]> members = entry.getFormattedStructMembers();
	    for (String[] member : members) {
	        TableItem item = new TableItem(structTable, SWT.NONE);
	        item.setText(new String[]{member[0], member[1], member[2]});
	    }
	}

}